package top.aprillie.mamabike.security;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;

/**
 * @Author: illure
 * @Description: AES对称加密
 * @Date: Created in 21:59 2018/2/9
 * @Modified By:
 */
public class AESUtil {

    public static final String KEY_ALGORITHM = "AES";
    public static final String KEY_ALGORITHM_MODE = "AES/CBC/PKCS5Padding";


    /**
     * AES对称加密  key要16位
     *
     * @param data
     * @param key
     * @return
     */
    public static String encrypt(String data, String key) {
        try {
            SecretKeySpec spec = new SecretKeySpec(key.getBytes("UTF-8"), KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_MODE);
            cipher.init(Cipher.ENCRYPT_MODE, spec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
            byte[] bs = cipher.doFinal(data.getBytes("UTF-8"));
            return Base64Util.encode(bs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * AES对称加密  key要16位
     *
     * @param data
     * @param key
     * @return
     */
    public static String decrypt(String data, String key) {
        try {
            SecretKeySpec spec = new SecretKeySpec(key.getBytes("UTF-8"), KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_MODE);
            cipher.init(Cipher.DECRYPT_MODE, spec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
            byte[] originBytes = Base64Util.decode(data);
            byte[] result = cipher.doFinal(originBytes);
            return new String(result, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        /**AES加密数据**/

        String key = "123456789abcdefg";
        String dataToEn = "{'mobile':'18752065699','code':'6666','platform':'android','channelId':12454348}";
        String enResult = encrypt(dataToEn, key);
        System.out.println("被加密的数据：" + enResult);
//        String deResult = decrypt(enResult, key);
//        System.out.println(deResult);
        /**RSA加密AES的密钥**/
        byte[] enKey = RSAUtil.encryptByPublicKey(key.getBytes("UTF-8"), RSAUtil.PUBLIC_KEY);
        //System.out.println(new String(enKey, "UTF-8"));
        String baseKey = Base64Util.encode(enKey);
        System.out.println("AES被RSA加密后的密钥：" + baseKey);
        /**服务端RSA解密AES的key**/
        byte[] de = Base64Util.decode(baseKey);
        byte[] deKeyResult = RSAUtil.decryptByPrivateKey(de);
        System.out.println("用来加密AES的key：" + new String(deKeyResult, "UTF-8"));
        String deResult = decrypt(enResult, key);
        System.out.println("原数据：" + deResult);


        /*
        AES加密数据、RSA加密AES的key的流程

        定义【AES的key】
        【AES加密数据通过key】AES加密数据返回先被AES加密后被BASE64工具编码后的数据     --客户端发送到服务端的数据
        【AES解密数据通过key】AES解密数据返回先被BASE64工具解码后被AES解密后的数据

        【通过openssl生成公钥和私钥】
        【RSA加密通过公钥】RSA加密【AES的key】先被BASE64工具解码后被RSA加密后的key      --客户端发送到服务端的key
        【RSA解密通过私钥】RSA解密【AES的key】先被BASE64工具解码后被PKCS8格式化然后被RSA解密的key

         */
    }
}
